/**
 * 动画特效播放类
 */
class MovieClip extends egret.MovieClip {
	private _mcFactory: egret.MovieClipDataFactory = null;

	private _playCount: number = null;
	private _fileName: string = null;
	private _jsonData: string = null;
	private _texture: egret.Texture = null;

	/**
	 * 安防层级
	 */
	private addIdex: number;

	private _callFunc: Function = null;
	private _thisObj: any = null;
	public constructor() {
		super();
		this.touchEnabled = false;
		this._mcFactory = new egret.MovieClipDataFactory();
		this.addListener();
	}

	private addListener(): void {
		this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoveFromeStage, this);
		this.addEventListener(egret.Event.COMPLETE, this.onComplete, this);
	}

	public removeListener(): void {
		this.removeEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoveFromeStage, this);
		this.removeEventListener(egret.Event.COMPLETE, this.onComplete, this);
	}

	private onComplete(evt: any): void {
		this.destroy();
	}

	private onRemoveFromeStage(evt: any): void {
		this.destroy();
	}

	public loadAndPlay(fileName: string, count: number): void {
		this._fileName = fileName;
		this._playCount = count;

		if (!this._fileName)
			return;

		var self = this;
		this._jsonData = null;
		this._texture = null;
		RES.getResAsync(fileName + "_json", function (data: any) {
			self._jsonData = data;
			self.createBody();
		}, this);

		RES.getResAsync(fileName + "_png", function (data: any) {
			self._texture = data;
			self.createBody();
		}, this);
	}

	private createBody(): void {
		if (this._jsonData
			&& this._texture
			&& this._fileName
			&& this._playCount) {
			this._mcFactory.mcDataSet = this._jsonData;
			this._mcFactory.texture = this._texture;
			this.movieClipData = this._mcFactory.generateMovieClipData();
			this.gotoAndPlay(0, this._playCount);
		}
	}

	public setPos(posX: number, posY: number): void {
		this.x = posX;
		this.y = posY;
	}

	/**
	 * 设置动画完成事件的监听
	 */
	public setCB(cb: Function, thisObj: any): void {
		this._callFunc = cb;
		this._thisObj = thisObj;
	}

	public clearMCCache(): void {
		this.stop();
		this._fileName = null;
		this._jsonData = null;
		this._texture = null;
		this._playCount = null;

		this._mcFactory.clearCache();
		this._mcFactory.mcDataSet = null;
		this._mcFactory.texture = null;
		this.movieClipData = null;

	}

	public destroy(): void {
		this.clearMCCache();
		this.removeListener();
		// DisplayUtils.getIt().removeFromParent(this);
		if (this.parent) {
			this.parent.removeChild(this);
		}

		if (this._callFunc && this._thisObj) {
			this._callFunc.apply(this._thisObj);
		}

		this._callFunc = null;
		this._thisObj = null;
	}

	public getMCFactory(): egret.MovieClipDataFactory {
		return this._mcFactory;
	}

	public static create(parentNode: any, resName: string, posX: number, posY: number, count: number = -1, index?: any): MovieClip {
		var mc: MovieClip = new MovieClip();
		if (index) {
			parentNode.addChildAt(mc, index);
		} else {
			parentNode.addChild(mc);
		}
		mc.setPos(posX, posY);
		mc.loadAndPlay(resName, count);
		return mc;
	}

}